﻿@model OT.Areas.Admin.Models.Topics
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
    <h3 id="myModalLabel">Edit Topic</h3>
</div>
@Html.HiddenFor(model => model.TopicID, new { id = "HDFID" })
<div class="modal-body">
    <div id="alert_modal">
        @Html.ValidationSummary()
    </div>
    <div class="form-horizontal" role="form">
        <div class="form-group">
            <div class="col-lg-15">
                <div id="divMsgEdit"></div>
            </div>
        </div>
        <div class="form-group" id="divCategoryName">
            <label class="col-lg-3 control-label">Category Name</label>
            <div class="col-lg-5">
                @Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.category, "CategoryID", "CategoryName"), "----Select----", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CategoryID)
            </div>
        </div>
        <div class="form-group" id="divSubjectName">
            <label class="col-lg-3 control-label">Subject Name</label>
            <div class="col-lg-5">
                @Html.DropDownListFor(model => model.SubjectID, new SelectList(Model.Subjects, "SubjectID", "SubjectName"), "--select--", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.SubjectID)
            </div>
        </div>
        <div class="form-group" id="divTopicName">
            <label class="col-lg-3 control-label">Topic Name</label>
            <div class="col-lg-5">
                @Html.TextBoxFor(model => model.TopicName, new { @maxlength = "10", @class = "form-control", @placeholder = "Enter Topic Name" })
                @Html.ValidationMessageFor(model => model.TopicName)
            </div>
        </div>
        <div class="form-group" id="divDescription">
            <label class="col-lg-3 control-label">Description</label>
            <div class="col-lg-5">
                <textarea cols="2" maxlength="50" id="Description" class="form-control" placeholder="Enter Description">@Model.Description</textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 control-label">Status</label>
            <div class="col-lg-1">
                @Html.EditorFor(model => model.Status)
            </div>
        </div>
       @* <div class="form-group" id="divUser">
            <label class="col-lg-3 control-label">User Name</label>
            <div class="col-lg-5">
                @Html.DropDownListFor(model => model.UserID, new SelectList(Model.Users, "UserID", "FirstName"), "--Select--", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.UserID)
            </div>
        </div>*@
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-default" type="submit" id="btnEdit"><i class="icon-cog"></i>Save</button>
    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i>Cancel</button>
</div>
<script type="text/javascript">
    $("#btnEdit").click(function (e) {
        var TopicID = $("#HDFID").val();
        var TopicName = $("#TopicName").val();
        var Description = $("#Description").val();
        var Category = $("#CategoryID").val();
        if ($("#Status").is(':checked')) {
            Status = true;
        }
        else {
            Status = false;
        }
        var SubjectID = $("#SubjectID").val();
        var UserID = 0;
        e.preventDefault();
        if (TopicName == '' || SubjectID == 0 || Category == 0 ) {
            showMsg('divMsgEdit', 'Please Enter Required Value', 'danger');
            (TopicName == '') ? $('#divTopicName').addClass('has-error') : $('#divTopicName').removeClass('has-error');
            (Category == 0) ? $('#divCategoryName').addClass('has-error') : $('#divCategoryName').removeClass('has-error');
            (SubjectID == 0) ? $('#divSubjectName').addClass('has-error') : $('#divSubjectName').removeClass('has-error');
            //(UserID == 0) ? $('#divUser').addClass('has-error') : $('#divUser').removeClass('has-error');
        }
        else {
            $.ajax({
                url: "/Admin/Topic/Edit?TopicID=" + TopicID + "&TopicName=" + TopicName + "&SubjectID=" + SubjectID + "&Description=" + Description + "&Status=" + Status + "&UserID=" + UserID,
                type: "POST",
                success: function (result) {
                    if (result != "NotValid") {
                        $('#modalDiv').modal('hide');
                        showMsg('divMsgIndex', 'Sucessfully Updated Record.', 'success');
                        loadPage(1, 10, '');
                    }
                    else {
                        showMsg('divMsgEdit', 'Failed to Update Record', 'danger');
                    }
                },
                error: function (data) {
                }
            });
        }
        return false;
    });

    $("#CategoryID").change(function (e) {
        var CategoryID = $(this).val();
        var sub = $("#SubjectID").empty();
        if (CategoryID > 0) {
            $.getJSON("/Admin/Topic/LoadSubject", { CategoryID: CategoryID },
              function (classesData) {
                  var select = $("#SubjectID");
                  select.empty();
                  select.append($('<option/>', {
                      value: 0,
                      text: "----Select Subject----"
                  }));
                  $.each(classesData, function (index, itemData) {

                      select.append($('<option/>', {
                          value: itemData.Value,
                          text: itemData.Text
                      }));
                  });
              });
        }

        return false;
    });

    $("#SubjectID").change(function (e) {
        var SubjectID = $(this).val();
        var sub = $("#TopicID").empty();
        if (SubjectID > 0) {
            $.getJSON("/Admin/Question/LoadTopic", { SubjectID: SubjectID },
                   function (classesData) {
                       var select = $("#TopicID");
                       select.append($('<option/>', {
                           value: 0,
                           text: "----Select Topic----"
                       }));
                       $.each(classesData, function (index, itemData) {

                           select.append($('<option/>', {
                               value: itemData.Value,
                               text: itemData.Text
                           }));
                       });
                   });
        }
        return false;
    });
</script>
